<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *线下卡使用记录
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$merchid=$this->checklogin();
if($_RQ['op']=='list'){	
	$params=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']);	
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	if(isset($_RQ['id'])){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	$total='';
	$fields=array('id','merchid','uniacid','code_sn','hxmid','symoney','createtime');
	$list=pdo_getslice('jy_kabao_sylog',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		//创建时间
		$params=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']);	
		if($_RQ['createtime']){
			$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
			$time_range=json_decode($time_range,true);
			$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
			$return['params']['createtime']=$time_range;
		}
		foreach($list as $k=>&$v){
		    $v['wname'] = pdo_getcolumn('jy_merch_worker',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$v['hxmid'],'merchid'=>$v['merchid']),'title');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'list'=>array('data'=>'list','delall'=>false,'del_one'=>false,'edit'=>false,'list'=>array(
			array('name'=>'code_sn','title'=>'卡号','type'=>'string'),
			array('name'=>'wname','title'=>'核销店员','type'=>'string'),
			array('name'=>'symoney','title'=>'使用金额','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
		    array('type'=>'href','op'=>'cartlog','title'=>'查看指定商品使用记录'),
		)),
		'search'=>array(
	
			array('name'=>'createtime','width'=>15,'title'=>'创建时间','type'=>'time_range'),
		)
	);
	$return['htmls']=$htmls;	
	exi($return);
}

if($_RQ['op']=='cartlog'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;

	if($_RQ['id']){
		
		$params=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'syid'=>$_RQ['id']);
	}
	
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}

	$list=pdo_getall('jy_weishop_member_kbcart',$params);
	if($list){
		foreach($list as &$item){
            $item['member']=model('jy_merch/member')->member_show($item['mid'],$merchid);
			$goodstitle = pdo_getcolumn('jy_weishop_goods',array('id'=>$item['goodsid'],'uniacid'=>$_SESSION['uniacid']),'title');
			$item['goodstitle']=$goodstitle;
		}
	}

	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export2',
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'核销人员信息','type'=>'member'),
			array('name'=>'kbid','title'=>'卡号','type'=>'string'),
			array('name'=>'goodstitle','title'=>'商品名称','type'=>'string'),
			array('name'=>'total','title'=>'采购数量','type'=>'string'),
			array('name'=>'price','title'=>'单价','type'=>'string'),
		))
	);
	$return['htmls']=$htmls;
	
	exi($return);
}

if($_RQ['op']=='export'){
	$params=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']);	
	if(isset($_RQ['id'])){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$list=pdo_getall('jy_kabao_sylog',$params);
	paixu($list,'id','desc');
	if($list){
		//创建时间
		$params=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']);	
		if($_RQ['createtime']){
			$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
			$time_range=json_decode($time_range,true);
			$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
			$return['params']['createtime']=$time_range;
		}
		foreach($list as $k=>&$v){
			$v['wname'] = pdo_getcolumn('jy_merch_worker',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$v['hxmid'],'merchid'=>$v['merchid']),'title');
		    $v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
		}
	}
	$header=array(
		'code_sn'=>'卡号',
		'wname'=>'核销人员',
		'symoney'=>'使用金额',
		'createtime'=>'使用时间',
	);
	cfc('tools')->export2excel($header,$list,'特殊线下卡使用统计'.date('YmdHis').rand(1,999));
}
